package com.example.shouybl.fragment;


import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.GiftMobileAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.GiftData;
import com.example.shouybl.constant.Path;

import com.example.shouybl.ui.GiftDetailActivity;
import com.example.shouybl.utils.AppAsyncTaskHelper;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;



import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftMobilFragment extends Fragment {

    public static final String ARGS_KEY="type";
    public static final String INTENT_NAME="id";
    private ListView lvGiftMobile;
    private List<GiftData> dataList= new ArrayList<GiftData>();;
    private AsyncHttpClient client = new AsyncHttpClient();
    private GiftMobileAdapter mobileAdapter;
    private int page = 1;
    private PullToRefreshListView pullMobile;
    private AppBaseBean baseBean;
    private String type;

   public static GiftMobilFragment newInstance(String type) {

       Bundle args = new Bundle();
       args.putString(ARGS_KEY, type);
       GiftMobilFragment fragment = new GiftMobilFragment();
       fragment.setArguments(args);
       return fragment;
   }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type=getArguments().getString(ARGS_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift_mobil, container, false);

        initView(view);
        initData();

        pullMobile.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initData();
            }
        });
        lvGiftMobile.setAdapter(mobileAdapter);
        lvGiftMobile.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(BaseApp.getInstance(), GiftDetailActivity.class);
                intent.putExtra(INTENT_NAME,dataList.get(position-1).getId());
                startActivity(intent);
            }
        });

        return view;
    }

    private void initData() {
       /* client.post(Path.PATH_GIFT, getParams(), new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                pullMobile.onRefreshComplete();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                baseBean= JSONObject.parseObject(responseString,AppBaseBean.class);
                if (baseBean!=null && baseBean.getState().equals("success")){
                    if (page==1 && !dataList.isEmpty()){
                        dataList.clear();
                    }
                    dataList.addAll(baseBean.getGiftDetInfo());
                }
                mobileAdapter.notifyDataSetChanged();
                pullMobile.onRefreshComplete();
            }
        });*/
        TypeReference<AppBaseBean<GiftData>> typeReference=new TypeReference<AppBaseBean<GiftData>>(){};
        AppAsyncTaskHelper.getInstance().post(Path.PATH_GIFT,getParams(),dataList,pullMobile,mobileAdapter,page,typeReference);
    }

    private RequestParams getParams(){
        RequestParams params=new RequestParams();
        params.add("uid", "");
        params.add("token", "78fc45f6f%2C7%2C9");
        params.add("platform", "2");
        params.add("gifttype", type);
        params.add("page", String.valueOf(page));
        return params;
    }

    private void initView(View view) {
        pullMobile = (PullToRefreshListView) view.findViewById(R.id.pull_mobile);
        pullMobile.setMode(PullToRefreshBase.Mode.BOTH);
        lvGiftMobile=pullMobile.getRefreshableView();
        mobileAdapter = new GiftMobileAdapter(dataList, BaseApp.getInstance());
    }


}
